package dreamCookGame.fishMiniGame;

import dreamCookGame.Item;
import dreamCookGame.Scene;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Timer;

	public class FishMiniGame extends Scene {

			Item[] _items;
			int _fishSpeeds[];
			public boolean _hooked;
			String[] _path;
			int[] _zigY;
			
			public FishMiniGame(String backgroundImgName, String foregroundImgName) {
				super("fishMiniGame", backgroundImgName, foregroundImgName, 0, 0, null, null, null);
				_items = new Item[10];
				createFish();
				_itemsBack = new Item[10];
				for(int i = 0; i < _itemsBack.length; i++)
					_itemsBack[i] = _items[i];
				
				_fishSpeeds = new int[_items.length-1];
				_hooked = false;
				_path = new String[_items.length-1];
				_zigY = new int[_items.length-1];
				setFishSpeeds();
				
				for(int i = 0; i < _items.length-1; i++){
					setPath(i);
				}

			}
			
			public void createFish(){			
				String[] goldFishImage = {"goldfish"};
				String[] fish1Image = {"fish1"};
				String[] fish2Image = {"fish2"};
				String[] fish3Image = {"fish3"};
				String[] fish4Image = {"fish4"};
				String[] fish5Image = {"fish5"};
				String[] fish6Image = {"fish6"};
				String[] fish7Image = {"fish7"};
				String[] fish8Image = {"fish8"};
				String[] fishingLine = {"fishingLineImage"};
				_items[0] = new Item("goldFish", goldFishImage, 100, 300, null, null, null, false, null);
				_items[1] = new Item("fish1", fish1Image, 200, 300, null, null, null, false, null);
				_items[2] = new Item("fish2", fish2Image, 100, 50, null, null, null, false, null);
				_items[3] = new Item("fish3", fish3Image, 50, 400, null, null, null, false, null);
				_items[4] = new Item("fish4", fish4Image, 0, 550, null, null, null, false, null);
				_items[5] = new Item("fish5", fish5Image, 350, 220, null, null, null, false, null);
				_items[6] = new Item("fish6", fish6Image, 500, 350, null, null, null, false, null);
				_items[7] = new Item("fish7", fish7Image, 400, 400, null, null, null, false, null);
				_items[8] = new Item("fish8", fish8Image, 400, 400, null, null, null, false, null);
				_items[9] = new Item("fishingLine", fishingLine, 400, -100, null, null, null, false, null);

			}
			
			public void setPath(int i){
				int movement;
				double rand = Math.random();
				
				if (rand < 0.5)
					movement = 0;
				else
					movement = 1;
				
				if(i%2 == 0 && movement == 0){
					_path[i] = "right";
					_items[i]._x = -100;
			      }
				
				else if(i%2 == 1 && movement == 0){
					_path[i] = "left";
					_items[i]._x = 800;
				}
				
				else if(i%2 == 0 && movement == 1){
					_path[i] = "zigZagRight";
					_items[i]._x = -100;
					_zigY[i] = (int)(Math.random()*200);
				}
				
				else{
					_path[i] = "zigZagLeft";
					_items[i]._x = 800;
					_zigY[i] = (int)(Math.random()*200);
				}			
			}
			
			public void setFishSpeeds(){
				for(int i = 0; i < _items.length-1; i++){
					_fishSpeeds[i] = (int) (10*Math.random());
				}
			}
			
			public void moveFish(){
				if(_hooked){
					_items[0]._x = _items[_items.length-1]._x-140;
					_items[0]._y = _items[_items.length-1]._y + 460;			
				}
				
				else{
					if(_items[0]._x > -150 && _items[0]._x < 950){
						_path[0] = "right";
						_items[0]._x += _fishSpeeds[0];
						
						
						/*else if(_path[0] == "left"){
							_items[0]._x -= _fishSpeeds[0];
						}
											
						else if(_path[0] == "zigZagright"){
							_items[0]._x += 20;
							_items[0]._y = 100+(int)(_items[0]._y*Math.sin(3.14*_items[0]._x /400));
						}*/
					}
					
					else{
						_items[0]._y = (int) (500*Math.random())+50;
						setPath(0);
					}
				}
				
				for(int i = 0; i < _items.length-1; i++){
					if(_items[i]._x > -150 && _items[i]._x <= 850){
						if(_path[i] == "right")
							_items[i]._x += _fishSpeeds[i];
						
						else if(_path[i] == "left")
							_items[i]._x -= _fishSpeeds[i];
						
						else if(_path[i] =="circle")
							_items[i]._x -= _fishSpeeds[i];
											
						else if(_path[i] == "zigZagRight"){
							_items[i]._x += 20;
							_items[i]._y = _zigY[i] + (int)(200+30*Math.sin(_items[i]._x));
						}
						
						else{
							_items[i]._x -= 20;
							_items[i]._y =  _zigY[i] + (int)(200+30*Math.sin(_items[i]._x));
						}
					}
					
					else{
						_items[i]._y = (int) (500*Math.random())+50;
						setPath(i);
					}
				}
			}
			
			public void moveFishingLine(char direction){
				switch(direction) {
				case 'r':
					_items[_items.length-1]._x += 10;
					break;
				case 'l':
					_items[_items.length-1]._x -= 10;
					break;
				case 'u':
					_items[_items.length-1]._y -= 10;
					break;
				case 'd':
					_items[_items.length-1]._y += 10;
					break;
				}
			
			}
			
			public void checkIntersections(){
				for(int i = 1; i < _items.length - 1; i++){
					if(_items[i].isWithinItem(_items[_items.length-1]._x+25, _items[_items.length-1]._y+550)){
							_hooked = false;
					}
			    }
			}
	}
